package com.dingqb.ktdemo.lambda

import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
import java.nio.file.Files
import java.nio.file.Paths

fun main(args: Array<String>) {
	lambda();
	lambdafull();
	var path = "/Users/qingbo/Desktop/2019-3-7";
	var files = File(path).listFiles();
	files?.forEach { name -> println(name) }

	run {
		println("runn");
	}
	"".let {
		println("let");
	}
	var t = PairC("pair-1", "pair-2");
	with(t) {
		println(t.first);
		println("${t.second}");
		println("with 调用多个方法--${transform("Red")}");
	}
	t.apply {
		first = "123";
		second = "345"
		third = "789"
	}
	with(t) {
		println(t.first);
		println("${t.second}");
		println("with 调用多个方法--${transform("Red")}");
	}
	ffx();


}


fun ffx() {
	try {
		//TODO("todo");
		var t = 0 / 0;
	} catch (e: Exception) {
		println(e.message);
		System.err.println(e.message);
	}
	var s = System::class;
	println(s.toString());


}

class PairC(var first: String, var second: String) {
	var third: String = "";
}

fun transform(color: String): Int {
	return when (color) {
		"Red" -> 10
		"Green" -> 11
		"Blue" -> 12
		else -> throw IllegalArgumentException("Invalid color param value")
	}
}

fun lambda() {
	val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
	fruits// 简要的写法
		.filter { it.startsWith("a") }
		.sortedBy { it }
		.map { it.toUpperCase() }
		.forEach { println(it) }
}


fun lambdafull() {
	val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
	fruits// 标准的写法
		.filter({ ittt -> ittt.startsWith("a") })
		.filter({ it.startsWith("a") })
		.filter({ it -> it.startsWith("a") })
		.sortedBy { itt -> itt }
		.map { itt -> itt.toUpperCase() }
		.forEach { itt -> println(itt) }
}
